package com.yoosora.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yoosora.common.consts.ConstsI;
import com.yoosora.common.except.CustomerException;
import com.yoosora.common.mode.BasicError;
import com.yoosora.common.mode.Operators;
import com.yoosora.common.mode.request.IDOperatorRequest;
import com.yoosora.common.mode.request.IDQueryRequest;
import com.yoosora.common.mode.request.plugin.PluginOtherRequest;
import com.yoosora.common.mode.request.plugin.PluginPageRequest;
import com.yoosora.common.mode.request.plugin.PluginSaveRequest;
import com.yoosora.common.mode.response.BasicResponse;
import com.yoosora.common.mode.response.MultiResponse;
import com.yoosora.common.mode.response.PageableResponse;
import com.yoosora.common.mode.response.SingleResponse;
import com.yoosora.dao.dataobj.PluginDO;
import com.yoosora.model.PluginDTO;
import com.yoosora.repository.PluginRepository;
import com.yoosora.service.PluginService;
import com.yoosora.utils.Collections;
import com.yoosora.utils.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author yoosora
 */
@Service
public class PluginServiceImpl implements PluginService {

	@Autowired
	private PluginRepository pluginRepository;
	
	private PluginDO toRecord(PluginSaveRequest request){
		PluginDO record = new PluginDO();
		record.setId(request.getId());
		record.setName(request.getName());
		record.setPackageName(request.getPackageName());
		record.setDetail(request.getDetail());
		record.setIsActive(request.getIsActive());
		return record;
	}
	
	@Override
	public SingleResponse<Long> save(PluginSaveRequest request) {
		final PluginDO record = null != request ? toRecord(request) : null;
		if(null != record){
			final Operators operators = request.getOperators();
			Long id = record.getId();
			if(null == id){
				pluginRepository.insert(record, operators);
				id = record.getId();
			}else{
				pluginRepository.update(record, operators);
			}
			return SingleResponse.<Long>newBuilder().succ(id).build();
		}
		throw new CustomerException(BasicError.ILLEGAL_REQUEST);
	}

	@Override
	public BasicResponse delete(IDOperatorRequest<Long> request) {
		if(null != request.getId()){
			final Operators operators = request.getOperators();
			if(!pluginRepository.delete(request.getId(), operators)){
				throw new CustomerException(BasicError.SYSTEM_ERROR);
			}
			return BasicResponse.newBuilder().build();
		}
		throw new CustomerException(BasicError.ILLEGAL_REQUEST);
	}

	@Override
	public SingleResponse<PluginDTO> queryByPK(IDQueryRequest<Long> request) {
		final Long id = null != request ? request.getId() : null;
		if(null != id){
			final PluginDTO record = pluginRepository.queryByPK(id);
			return SingleResponse.<PluginDTO>newBuilder().succ(record).build();
		}
		throw new CustomerException(BasicError.ILLEGAL_REQUEST);
	}

	@Override
	public PageableResponse<PluginDTO> queryPage(PluginPageRequest request) {
		final int page = null != request ? request.getPage() : ConstsI.ONE;
		final int rows = null != request ? request.getRows() : ConstsI.FIVE;

		PluginDO entity = new PluginDO();
		entity.setDeleted(Boolean.FALSE);
		QueryWrapper<PluginDO> qw = new QueryWrapper<>(entity);
		qw.orderByAsc("created_date");

		long total = pluginRepository.count(qw);
		List<PluginDTO> dtos = Collections.emptyList();
		if(0 < total) {
			Page<PluginDO> iPage = new Page<>(page, rows, false);
			dtos = pluginRepository.queryPage(iPage, qw);
		}
		return PageableResponse.<PluginDTO>newBuilder(page, rows).succ(dtos, total).build();
	}

	@Override
	public MultiResponse<PluginDTO> queryList(PluginOtherRequest request) {
		PluginDO entity = null != request ? conv(request) : null;
		entity.setDeleted(Boolean.FALSE);
		QueryWrapper<PluginDO> qw = new QueryWrapper<>(entity);
		qw.orderByAsc("created_date");
		if(Collections.isNotEmpty(request.getIds())){
			qw.in("id", request.getIds());
		}
		List<PluginDTO> dtos = pluginRepository.queryWrapper(qw);
		return MultiResponse.<PluginDTO>newBuilder().succ(dtos).build();
	}
	public PluginDO conv(PluginOtherRequest request){
		PluginDO entity = new PluginDO();
		if(null != request.getId()){
			entity.setId(request.getId());
		}
		if(Strings.isNotEmpty(request.getName())){
			entity.setName(request.getName());
		}
		if(Strings.isNotEmpty(request.getPackageName())){
			entity.setPackageName(request.getPackageName());
		}
		if(Strings.isNotEmpty(request.getDetail())){
			entity.setDetail(request.getDetail());
		}
		if(null != request.getIsActive()){
			entity.setIsActive(request.getIsActive());
		}
		return entity;
	}
}
